/**
 * @ClassName KP26_PriorityQueueComparator
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/25 15:35
 */

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 年龄比较器
 */
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

/**
 * 优先级队列比较器
 */
public class KP26_PriorityQueueComparator {
    public static void main(String[] args) {
        Student s1 = new Student(6, "张三", 33);
        Student s2 = new Student(2, "李四", 25);
        Student s3 = new Student(4, "王五", 17);
        Student s4 = new Student(3, "赵六", 47);
        Student s5 = new Student(5, "陈七", 37);
        Student s6 = new Student(1, "肖八", 75);
        // 1、自定义年龄比较器 升序 --> 小根堆
        //PriorityQueue<Student> students = new PriorityQueue<>(new AgeComparator());
        // 2、系统自带比较器
        /*PriorityQueue<Student> students = new PriorityQueue<>(new Comparator<Student>() {
            // 根据年龄降序排列
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });*/
        // 3、lambda表达式：升序排列
        PriorityQueue<Student> students = new PriorityQueue<>((o1, o2) -> {
            return o1.getAge() - o2.getAge();
        });
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s6);
        while (!students.isEmpty()) {
            System.out.println(students.poll());
        }
    }
}
